﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Me.Prettyprint.Cassandra.Model;
using Me.Prettyprint.Hector.Api.Query;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Cassandra.Service;
using Org.Apache.Cassandra.Thrift;
using Me.Prettyprint.Cassandra.Model.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// ThriftColumnQuery is used to query for a single column in a column family.
    /// Note we use generics to insure key, name and value serializers are correct.
    /// The class uses lambda expression to handle the query.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class ThriftColumnQuery<K,N,V> : AbstractColumnQuery<K,N,V>, ColumnQuery<K,N,V>
    {
        public ThriftColumnQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<N> nameSerializer, Serializer<V> valueSerializer) : 
            base (keyspace,keySerializer,nameSerializer,valueSerializer)
        {
        }
        public ThriftColumnQuery(Keyspace keyspace, Serializer<V> valueSerializer) : 
            base(keyspace,TypeInferringSerializer<K>.Instance,TypeInferringSerializer<N>.Instance,valueSerializer)
        {
        }
        public ThriftColumnQuery(Keyspace keyspaceOperator) : 
            base(keyspaceOperator,TypeInferringSerializer<K>.Instance,
            TypeInferringSerializer<N>.Instance,(Serializer<V>)StringSerializer.Instance)
        {
        }
        public override QueryResult<HColumn<N, V>> Execute()
        {
            Func<KeyspaceService,HColumnImpl<N, V>> x = (KeyspaceService ks) => {

              Column thriftColumn = ks.GetColumn(KeySerializer.ToBytes(Key),
                  ThriftFactory.CreateColumnPath(ColumnFamilyName, Name, ColumnNameSerializer));
              return new HColumnImpl<N, V>(thriftColumn, ColumnNameSerializer, ValueSerializer);
        };
            return new QueryResultImpl<HColumn<N, V>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<HColumn<N, V>>(x)), this);
        }
    }
}
